import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart' hide SnackPosition;
import 'package:flutter_svg/flutter_svg.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import '../../common/index.dart';
import 'index.dart';

class PostDetailPage extends GetView<PostDetailController> {
  const PostDetailPage({super.key});

  @override
  Widget build(BuildContext context) {
    // 获取路由参数
    final arguments = GoRouterState.of(context).extra;

    // 检查控制器中是否已有帖子数据
    final existing_post = controller.get_current_post();

    // 处理参数类型问题
    PostModel? post_from_args;
    bool is_loading = false;

    if (arguments != null) {
      if (arguments is PostModel) {
        // 直接使用PostModel类型的参数
        post_from_args = arguments;
      } else if (arguments is Map) {
        // 如果是Map类型，尝试获取postId并加载帖子数据
        try {
          final Map argsMap = arguments;
          if (argsMap.containsKey('postId') || argsMap.containsKey('userId')) {
            // 标记为加载状态
            is_loading = true;

            // 创建一个立即加载的函数
            Future<void> loadPostData() async {
              try {
                final postId = argsMap.containsKey('postId')
                    ? argsMap['postId'].toString()
                    : null;

                PostModel? loadedPost;

                // 如果有postId，直接加载帖子
                if (postId != null) {
                  loadedPost = await PostService().get_post_by_id(postId);
                }
                // 否则如果有userId，尝试获取该用户的第一个帖子
                else if (argsMap.containsKey('userId')) {
                  final userId = argsMap['userId'].toString();
                  final user = UserService().get_user_data_by_id(userId);
                  if (user != null &&
                      user.post_lists != null &&
                      user.post_lists!.isNotEmpty) {
                    loadedPost = await PostService().get_post_by_id(
                      user.post_lists!.first,
                    );
                  }
                }

                if (loadedPost != null) {
                  // 更新控制器中的帖子数据
                  controller.set_current_post(loadedPost);
                  // 刷新UI
                  controller.update(["post_detail"]);
                } else {
                  // 如果加载失败，使用WidgetsBinding确保在构建完成后执行
                  WidgetsBinding.instance.addPostFrameCallback((_) {
                    // 返回上一页
                    RouteUtil.pop();
                    // 显示提示
                    Future.delayed(Duration(milliseconds: 300), () {
                      SmartDialogHelper.show_snackbar(
                        title: 'Error',
                        message: 'Failed to load post details',
                        backgroundColor: Colors.red,
                        colorText: Colors.white,
                      );
                    });
                  });
                }
              } catch (e) {
                print('加载帖子数据错误: $e');
                // 使用WidgetsBinding确保在构建完成后执行
                WidgetsBinding.instance.addPostFrameCallback((_) {
                  // 返回上一页
                  RouteUtil.pop();
                  // 显示提示
                  Future.delayed(Duration(milliseconds: 300), () {
                    SmartDialogHelper.show_snackbar(
                      title: 'Error',
                      message: 'An error occurred while loading post',
                      backgroundColor: Colors.red,
                      colorText: Colors.white,
                    );
                  });
                });
              }
            }

            // 立即开始加载，但在下一帧执行而不是当前构建过程中
            WidgetsBinding.instance.addPostFrameCallback((_) {
              loadPostData();
            });
          }
        } catch (e) {
          print('解析参数错误: $e');
        }
      }
    }

    // 如果正在加载数据，显示加载界面
    if (is_loading) {
      return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0,
          leading: IconButton(
            icon: Icon(Icons.arrow_back, color: Colors.white),
            onPressed: () => RouteUtil.pop(),
          ),
        ),
        extendBodyBehindAppBar: true,
        body: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: AssetImage(AssetsImages.bg),
              fit: BoxFit.cover,
            ),
          ),
          child: Center(
            child: CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation<Color>(Color(0xFFCDFF5F)),
            ),
          ),
        ),
      );
    }

    // 优先使用有效的PostModel参数，其次使用控制器中缓存的数据
    // 如果都没有，显示错误
    if (post_from_args == null && existing_post == null) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        RouteUtil.pop();
        SmartDialogHelper.show_snackbar(
          title: 'Error',
          message: 'No post data available',
          backgroundColor: Colors.red,
          colorText: Colors.white,
        );
      });
      return Scaffold(body: Center(child: CircularProgressIndicator()));
    }

    final PostModel post = post_from_args ?? existing_post!;

    // 将帖子数据保存到控制器中
    WidgetsBinding.instance.addPostFrameCallback((_) {
      controller.set_current_post(post);
    });

    final user = UserService().get_user_data_by_id(post.user_id);
    final String avatar_path = user?.avatar_image ?? 'assets/images/avatar.jpg';
    final String usename = user?.name ?? 'Unknown';
    final page_controller = PageController();
    final scroll_controller = ScrollController();
    final image_height_notifier = ValueNotifier<double>(470.h);
    final initial_image_height = 470.h;
    final min_image_height = initial_image_height / 5.0;
    final image_path_handler = ImagePath();
    // 添加评论输入控制器
    final comment_controller = TextEditingController();

    // 每次进入详情页时强制刷新评论 - 修复不同界面进入看到的评论不一致问题
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      try {
        // 先获取最新的帖子数据
        final updated_post = await PostService().get_post_by_id(post.post_id);
        if (updated_post != null) {
          // 更新控制器中的帖子数据
          controller.set_current_post(updated_post);
          // 刷新评论列表
          await controller.refreshComments(updated_post.post_content_list);
          // 刷新UI
          controller.update(["post_detail"]);
        }
      } catch (e) {
        print('刷新帖子评论错误: $e');
      }
    });

    scroll_controller.addListener(() {
      double scroll_offset = scroll_controller.offset;
      double new_height = initial_image_height - scroll_offset;

      if (new_height > initial_image_height) {
        new_height = initial_image_height;
      } else if (new_height < min_image_height) {
        new_height = min_image_height;
      }

      image_height_notifier.value = new_height;
    });

    return GetBuilder<PostDetailController>(
      id: "post_detail",
      builder: (_) {
        return GestureDetector(
          // 点击空白处关闭键盘
          onTap: () => FocusScope.of(context).unfocus(),
          child: Scaffold(
            resizeToAvoidBottomInset: false,
            backgroundColor: Colors.white,
            body: SafeArea(
              top: false,
              bottom: false,
              child: Stack(
                children: [
                  // 主体内容
                  Container(
                    width: double.infinity,
                    height: double.infinity,
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage(AssetsImages.bg),
                        fit: BoxFit.cover,
                      ),
                    ),
                    child: Column(
                      children: [
                        // 顶部内容区域
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            SizedBox(height: 55.h),
                            Row(
                              children: [
                                SizedBox(width: 18.w),
                                InkWell(
                                  onTap: () => RouteUtil.pop(),
                                  child: Container(
                                    height: 40.h,
                                    width: 40.w,
                                    padding: EdgeInsets.all(10.r),
                                    decoration: BoxDecoration(
                                      color: Color(0xFFCDFF5F),
                                      borderRadius: BorderRadius.circular(10.r),
                                    ),
                                    child: SvgPicture.asset(
                                      AssetsImages.back,
                                      width: 20.w,
                                      height: 20.h,
                                    ),
                                  ),
                                ),
                                SizedBox(width: 100.w),

                                // 点击头像前往他人主页
                                InkWell(
                                  onTap: () {
                                    if (user != null) {
                                      debugPrint(
                                        "头像点击 - 用户数据: ${user.name}, ID: ${user.id}",
                                      );

                                      // 直接使用GoRouter API避免命名冲突
                                      GoRouter.of(
                                        context,
                                      ).push(RouteNames.others, extra: user);
                                    } else {
                                      SmartDialogHelper.show_snackbar(
                                        title: 'User not found',
                                        message: 'Unable to view user profile',
                                        backgroundColor: Colors.red,
                                        colorText: Colors.white,
                                      );
                                    }
                                  },
                                  child: CircleAvatar(
                                    radius: 20.r,
                                    backgroundImage: ImagePath().get_post_image(
                                      avatar_path,
                                    ),
                                  ),
                                ),

                                SizedBox(width: 10.w),
                                Text(
                                  usename,
                                  style: TextStyle(
                                    fontSize: 16.sp,
                                    fontWeight: FontWeight.w900,
                                    color: Colors.white,
                                  ),
                                ),
                                Spacer(),
                                Container(
                                  height: 40.h,
                                  width: 40.w,
                                  padding: EdgeInsets.all(10.r),
                                  decoration: BoxDecoration(
                                    color: Color(0xFFCDFF5F),
                                    borderRadius: BorderRadius.circular(10.r),
                                  ),
                                  child: InkWell(
                                    onTap: () => _show_report_dialog(
                                      context,
                                      post.post_id,
                                    ),
                                    child: SvgPicture.asset(
                                      AssetsImages.report,
                                      width: 20.w,
                                      height: 20.h,
                                    ),
                                  ),
                                ),
                                SizedBox(width: 24.w),
                              ],
                            ),
                            SizedBox(height: 20.h),
                            // 图片
                            ValueListenableBuilder<double>(
                              valueListenable: image_height_notifier,
                              builder: (context, image_height, child) {
                                return Stack(
                                  children: [
                                    SizedBox(
                                      height: image_height,
                                      width: double.infinity,
                                      child: FutureBuilder<List<String>>(
                                        future:
                                            ImagePath.process_post_image_list(
                                              post.post_image_list,
                                            ),
                                        builder: (context, snapshot) {
                                          if (!snapshot.hasData) {
                                            return const Center(
                                              child:
                                                  CircularProgressIndicator(),
                                            );
                                          }

                                          final imageList = snapshot.data!;
                                          return PageView(
                                            controller: page_controller,
                                            children: imageList.map((
                                              imagePath,
                                            ) {
                                              return Container(
                                                margin: EdgeInsets.symmetric(
                                                  horizontal: 8.w,
                                                ),
                                                decoration: BoxDecoration(
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                        20.r,
                                                      ),
                                                  image: DecorationImage(
                                                      image: image_path_handler
                                                        .get_post_image(
                                                          imagePath,
                                                        ),
                                                    fit: BoxFit.cover,
                                                  ),
                                                ),
                                              );
                                            }).toList(),
                                          );
                                        },
                                      ),
                                    ),

                                    // 点赞按钮
                                    Positioned(
                                      top: 20.h,
                                      right: 20.w,
                                      child: Container(
                                        width: 50.w,
                                        height: 50.h,
                                        decoration: BoxDecoration(
                                          color: Colors.black.withOpacity(0.7),
                                          shape: BoxShape.circle,
                                          boxShadow: [
                                            BoxShadow(
                                              color: Colors.black.withOpacity(
                                                0.25,
                                              ),
                                              spreadRadius: 2,
                                              blurRadius: 5,
                                              offset: Offset(0, 2),
                                            ),
                                          ],
                                        ),
                                        child: InkWell(
                                          onTap: () {
                                            controller.toggle_like(post.post_id);
                                            // 添加震动反馈
                                            HapticFeedback.mediumImpact();
                                          },
                                          child: Stack(
                                            alignment: Alignment.center,
                                            children: [
                                              // 背景波纹效果
                                              if (controller.is_liked(
                                                post.post_id,
                                              ))
                                                TweenAnimationBuilder<double>(
                                                  tween: Tween(
                                                    begin: 0.0,
                                                    end: 1.0,
                                                  ),
                                                  duration: Duration(
                                                    milliseconds: 700,
                                                  ),
                                                  builder: (context, value, child) {
                                                    return Opacity(
                                                      opacity:
                                                          (1 - value) * 0.8,
                                                      child: Transform.scale(
                                                        scale:
                                                            0.8 + value * 0.7,
                                                        child: Container(
                                                          decoration:
                                                              BoxDecoration(
                                                                shape: BoxShape
                                                                    .circle,
                                                                color: Color(
                                                                  0xFFCDFF5F,
                                                                ).withOpacity(0.5),
                                                              ),
                                                        ),
                                                      ),
                                                    );
                                                  },
                                                ),

                                              // 点赞图标
                                              AnimatedSwitcher(
                                                duration: Duration(
                                                  milliseconds: 300,
                                                ),
                                                transitionBuilder:
                                                    (child, animation) {
                                                      return ScaleTransition(
                                                        scale: CurvedAnimation(
                                                          parent: animation,
                                                          curve:
                                                              Curves.elasticOut,
                                                        ),
                                                        child:
                                                            RotationTransition(
                                                              turns: animation,
                                                              child: child,
                                                            ),
                                                      );
                                                    },
                                                child:
                                                    controller.is_liked(
                                                      post.post_id,
                                                    )
                                                    ? SvgPicture.asset(
                                                        AssetsImages.goodAfter,
                                                        key: ValueKey('liked'),
                                                        width: 30.w,
                                                        height: 30.h,
                                                        color: Color(
                                                          0xFFCDFF5F,
                                                        ),
                                                      )
                                                    : SvgPicture.asset(
                                                        AssetsImages.good,
                                                        key: ValueKey(
                                                          'unliked',
                                                        ),
                                                        width: 30.w,
                                                        height: 30.h,
                                                        color: Colors.white,
                                                      ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      ),
                                    ),

                                    // 标题和内容区域
                                    Positioned(
                                      bottom: 0,
                                      left: 8.w,
                                      right: 8.w,
                                      child: Container(
                                        decoration: BoxDecoration(
                                          borderRadius: BorderRadius.only(
                                            bottomLeft: Radius.circular(20.r),
                                            bottomRight: Radius.circular(20.r),
                                          ),
                                          gradient: LinearGradient(
                                            begin: Alignment.topCenter,
                                            end: Alignment.bottomCenter,
                                            colors: [
                                              Colors.transparent,
                                              Colors.black,
                                            ],
                                          ),
                                        ),
                                        child: Column(
                                          children: [
                                            Align(
                                              alignment: Alignment.topLeft,
                                              child: Text(
                                                post.post_title,
                                                style: TextStyle(
                                                  fontSize: 20.sp,
                                                  fontWeight: FontWeight.bold,
                                                  color: Colors.white,
                                                ),
                                              ),
                                            ),
                                            Text(
                                              post.post_detail,
                                              style: TextStyle(
                                                fontSize: 16.sp,
                                                color: Colors.white,
                                              ),
                                            ),
                                            SizedBox(height: 25.h),
                                          ],
                                        ).paddingOnly(left: 10.h),
                                      ),
                                    ),
                                  ],
                                );
                              },
                            ),

                            SizedBox(height: 10.h),

                            // 图片指示器
                            Center(
                              child: FutureBuilder<List<String>>(
                                future: ImagePath.process_post_image_list(
                                  post.post_image_list,
                                ),
                                builder: (context, snapshot) {
                                  if (!snapshot.hasData) {
                                    return SizedBox();
                                  }

                                  return SmoothPageIndicator(
                                    controller: page_controller,
                                    count: snapshot.data!.length,
                                    effect: ExpandingDotsEffect(
                                      dotHeight: 8.h,
                                      dotWidth: 16.w,
                                      activeDotColor: Color(0xFFCDFF5F),
                                      dotColor: Colors.grey.withOpacity(0.5),
                                    ),
                                  );
                                },
                              ),
                            ),
                          ],
                        ),
                        SizedBox(height: 10.h),
                        // 评论区
                        Expanded(
                          child: Container(
                            decoration: BoxDecoration(
                              color: Color(0xFFCDFF5F),
                              borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(30.r),
                                topRight: Radius.circular(30.r),
                              ),
                            ),
                            child: Column(
                              children: [
                                SizedBox(height: 20.h),
                                Padding(
                                  padding: EdgeInsets.only(left: 16.r),
                                  child: Align(
                                    alignment: Alignment.topLeft,
                                    child: Obx(
                                      () => Text(
                                        'Total ${controller.comments_list.length} Comments',
                                        style: TextStyle(
                                          fontSize: 18.sp,
                                          fontWeight: FontWeight.bold,
                                          color: Colors.black,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                                SizedBox(height: 16.h),
                                Expanded(
                                  child: Padding(
                                    padding: EdgeInsets.only(
                                      right: 20.w,
                                      // 为底部输入框预留空间
                                      bottom: 80.h,
                                    ),
                                    child: Obx(() {
                                      if (controller.comments_list.isEmpty) {
                                        return Center(
                                          child: Text(
                                            'No comments yet',
                                            style: TextStyle(
                                              fontSize: 16.sp,
                                              color: Colors.black54,
                                            ),
                                          ),
                                        );
                                      }

                                      return Scrollbar(
                                        controller: scroll_controller,
                                        thickness: 8.r,
                                        radius: Radius.circular(35.r),
                                        scrollbarOrientation:
                                            ScrollbarOrientation.right,
                                        thumbVisibility: true,
                                        interactive: true,
                                        child: ListView.builder(
                                          controller: scroll_controller,
                                          padding: EdgeInsets.only(
                                            left: 16.r,
                                            top: 0,
                                            bottom: 16.r,
                                          ),
                                          itemCount:
                                              controller.comments_list.length,
                                          itemBuilder: (context, index) {
                                            final comment =
                                                controller.comments_list[index];
                                            return Container(
                                              margin: EdgeInsets.only(
                                                bottom: 16.h,
                                              ),
                                              child: Row(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  InkWell(
                                                    onTap: () {
                                                      final userModel = UserService
                                                          .to
                                                          .get_user_data_by_id(
                                                            comment.user_id,
                                                          );
                                                      if (userModel != null) {
                                                        debugPrint(
                                                          "评论区头像点击 - 用户数据: ${userModel.name}, ID: ${userModel.id}",
                                                        );

                                                        // 直接使用GoRouter API避免命名冲突
                                                        GoRouter.of(
                                                          context,
                                                        ).push(
                                                          RouteNames.others,
                                                          extra: userModel,
                                                        );
                                                      } else {
                                                        SmartDialogHelper.show_snackbar(
                                                          title:
                                                              'User not found',
                                                          message:
                                                              'Unable to view user profile',
                                                          backgroundColor:
                                                              Colors.red,
                                                          colorText:
                                                              Colors.white,
                                                        );
                                                      }
                                                    },
                                                    child: Container(
                                                      width: 32.w,
                                                      height: 32.w,
                                                      decoration: BoxDecoration(
                                                        shape: BoxShape.circle,
                                                        image: DecorationImage(
                                                            image: image_path_handler
                                                              .get_avatar_image(
                                                                UserService.to
                                                                        .get_user_data_by_id(
                                                                          comment
                                                                              .user_id,
                                                                        )
                                                                        ?.avatar_image ??
                                                                    'assets/images/avatar.jpg',
                                                              ),
                                                          fit: BoxFit.cover,
                                                        ),
                                                      ),
                                                    ),
                                                  ),
                                                  SizedBox(width: 12.w),
                                                  Expanded(
                                                    child: Column(
                                                      crossAxisAlignment:
                                                          CrossAxisAlignment
                                                              .start,
                                                      children: [
                                                        Text(
                                                          UserService.to
                                                                  .get_user_data_by_id(
                                                                    comment
                                                                        .user_id,
                                                                  )
                                                                  ?.name ??
                                                              'Unknown',
                                                          style: TextStyle(
                                                            fontSize: 14.sp,
                                                            fontWeight:
                                                                FontWeight.bold,
                                                            color: Colors.black,
                                                          ),
                                                        ),
                                                        SizedBox(height: 4.h),
                                                        Text(
                                                          comment.comment_detail,
                                                          style: TextStyle(
                                                            fontSize: 14.sp,
                                                            color: Colors.black,
                                                          ),
                                                        ),
                                                      ],
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            );
                                          },
                                        ),
                                      );
                                    }),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),

                  // 输入框区域，使用Positioned固定在底部
                  Positioned(
                    left: 0,
                    right: 0,
                    // 根据键盘高度动态调整位置
                    bottom: MediaQuery.of(context).viewInsets.bottom,
                    child: Container(
                      padding: EdgeInsets.only(
                        left: 16.r,
                        right: 16.r,
                        top: 16.r,
                        bottom: 16.r + MediaQuery.of(context).padding.bottom,
                      ),
                      decoration: BoxDecoration(color: Color(0xFFCDFF5F)),
                      child: Row(
                        children: [
                          Expanded(
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 16.w),
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(20.r),
                              ),
                              child: TextField(
                                controller: comment_controller,
                                decoration: InputDecoration(
                                  hintText: 'Say something',
                                  border: InputBorder.none,
                                ),
                              ),
                            ),
                          ),
                          SizedBox(width: 10.w),
                          InkWell(
                            onTap: () async {
                              // 隐藏键盘
                              controller.hide_keyboard();

                              // 显示加载对话框
                              SmartDialog.showLoading(
                                alignment: Alignment.bottomCenter,
                              );

                              // 发送评论
                              final result = await controller.addition_comment(
                                post.post_id,
                                comment_controller.text.trim(),
                              );

                              // 关闭加载对话框
                              SmartDialog.dismiss();

                              if (result) {
                                // 清空输入框
                                comment_controller.clear();

                                // 显示成功消息
                                SmartDialogHelper.show_snackbar(
                                  title: 'Success',
                                  message: 'Comment published successfully',
                                  backgroundColor: Colors.green,
                                  colorText: Colors.white,
                                );
                              } else {
                                // 显示错误消息
                                SmartDialogHelper.show_snackbar(
                                  title: 'Error',
                                  message:
                                      'Comment publication failed, please try again',
                                  backgroundColor: Colors.red,
                                  colorText: Colors.white,
                                );
                              }
                            },
                            child: Container(
                              height: 45.h,
                              width: 75.w,
                              decoration: BoxDecoration(
                                color: Colors.black,
                                borderRadius: BorderRadius.circular(20.r),
                              ),
                              child: Center(
                                child: Text(
                                  "Send",
                                  style: TextStyle(
                                    fontSize: 14.sp,
                                    color: Colors.white,
                                    fontWeight: FontWeight.w600,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  // 从底部显示报告对话框
  void _show_report_dialog(BuildContext context, String postId) {
    // 检查用户是否登录
    final currentUser = UserService().get_now_user();
    if (currentUser == null) {
      // 提示用户登录
      SmartDialogHelper.show_snackbar(
        title: 'Instruction',
        message: 'Please login first before reporting posts.',
        backgroundColor: Colors.orange,
        colorText: Colors.white,
      );
      RouteUtil.push(RouteNames.login);
      return;
    }

    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      builder: (context) => Container(
        padding: EdgeInsets.all(16.r),
        decoration: BoxDecoration(
          color: Color(0xFFCDFF5F),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(30.r),
            topRight: Radius.circular(30.r),
          ),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              'Report',
              style: TextStyle(
                fontSize: 16.sp,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),
            SizedBox(height: 16.h),
            _build_report_button(context, 'Pornographic vulgarity', postId),
            SizedBox(height: 8.h),
            _build_report_button(context, 'Privacy Concerns', postId),
            SizedBox(height: 8.h),
            _build_report_button(context, 'Hate speech', postId),
            SizedBox(height: 8.h),
            _build_report_button(context, 'Nauseating', postId),
            SizedBox(height: 8.h),
            _build_report_button(context, 'Illegal', postId),
            SizedBox(height: 8.h),
            _build_report_button(context, 'Other', postId),
            SizedBox(height: 8.h),
            _build_report_button(context, 'Block', postId),
            SizedBox(height: 16.h),
            _build_report_button(context, 'Cancel', ''),
          ],
        ),
      ),
    );
  }

  // 构建单个报告按钮
  Widget _build_report_button(
    BuildContext context,
    String text,
    String postId,
  ) {
    return InkWell(
      onTap: () {
        if (text == 'Cancel') {
          Navigator.pop(context);
        } else {
          controller.handle_report(context, text, postId);
        }
      },
      child: Container(
        width: double.infinity,
        height: 30.h,
        decoration: BoxDecoration(
          color: text == 'Cancel' ? Colors.white : Colors.black,
          borderRadius: BorderRadius.circular(24.r),
        ),
        child: Center(
          child: Text(
            text,
            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.w600,
              color: text == 'Cancel' ? Colors.black : Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}
